package br.com.amadeus.android.dao;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

import br.com.amadeus.android.entities.Forum;
import br.com.amadeus.android.entities.Message;
import br.com.amadeus.android.util.Util;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

public class MessageDAO {
	
	public static String[] columns = new String[] {MessageDAO.ID, MessageDAO.BODY, MessageDAO.DATE, MessageDAO.PERSON_NAME,
		MessageDAO.FORUM_ID, MessageDAO.AMADEUS_ID, MessageDAO.PERSON_LOGIN};
	
	public static final String ID = "_id";
	public static final String BODY = "body";
	public static final String DATE = "date";
	public static final String PERSON_NAME = "person_name";
	public static final String FORUM_ID = "forum_id";
	public static final String AMADEUS_ID = "amadeus_id";
	public static final String PERSON_LOGIN = "person_login";
	
	private SQLiteDatabase db;
	
	private static MessageDAO instance = new MessageDAO();
	
	public static MessageDAO getInstance(Context ctx){
		if (instance.db == null || !instance.db.isOpen()) {
			instance.db = new DBHelper(ctx).getWritableDatabase();
		}
		return instance;
	}
	
	private MessageDAO(){
		
	}
	
	public long save(Message message){
		long id = message.getId();
		if (id == 0) {
			return insert(message);
		}else{
			return update(message);
		}
	}

	private long insert(Message message) {
		long id_message = -1;
		
		db.beginTransaction();
		try{
			SimpleDateFormat dtFormat = new SimpleDateFormat(
			"yyyy-MM-dd HH:mm");
			ContentValues values = new ContentValues();
			values.put(MessageDAO.BODY, message.getBody());
			values.put(MessageDAO.DATE, dtFormat.format(message.getDate()));
			values.put(MessageDAO.PERSON_NAME, message.getPersonName());
			values.put(MessageDAO.FORUM_ID, message.getForum().getId());
			values.put(MessageDAO.AMADEUS_ID, message.getAmadeus_id());
			values.put(MessageDAO.PERSON_LOGIN, message.getPersonLogin());
			
			id_message = db.insert(DBHelper.TABLE_MESSAGE, null, values);
			
			if (id_message != -1) {
				db.setTransactionSuccessful();
			}
			
		}finally{
			db.endTransaction();
		}
		
		
		return id_message;
	}

	private long update(Message message) {
		// TODO Auto-generated method stub
		return 0;
	}
	
	public void delete(int id){
		db.beginTransaction();
		try{
			db.delete(DBHelper.TABLE_MESSAGE, MessageDAO.ID + "=?", new String[]{String.valueOf(id)});
			db.setTransactionSuccessful();
		}finally{
			db.endTransaction();
		}
	}
	
	public ArrayList<Message> getListMessagesByForum(int forum_id_param){
		ArrayList<Message> listMessages = new ArrayList<Message>();
		Cursor c = db.query(DBHelper.TABLE_MESSAGE, columns, MessageDAO.FORUM_ID + "=" + forum_id_param, null, null, null, null);
		
		if (c.getCount() > 0) {
			c.moveToFirst();
			do {
				listMessages.add(fillMessage(c));
			} while (c.moveToNext());
		}
		c.close();
		return listMessages;
	}

	public static Message fillMessage(Cursor c) {
				
		int id = c.getInt(c.getColumnIndex(MessageDAO.ID));
		String body = c.getString(c.getColumnIndex(MessageDAO.BODY));
		Date date = Util.getDate(c.getString(c.getColumnIndex(MessageDAO.DATE)), Util.FORMATO_DATA_BD);
		String personName = c.getString(c.getColumnIndex(MessageDAO.PERSON_NAME));
		int forum_id = c.getInt(c.getColumnIndex(MessageDAO.FORUM_ID));
		int amadeus_id = c.getInt(c.getColumnIndex(MessageDAO.AMADEUS_ID));
		String personLogin = c.getString(c.getColumnIndex(MessageDAO.PERSON_LOGIN));
		
		Message message = new Message(id, body, date, personName, new Forum(forum_id), amadeus_id, personLogin);
				
		return message;
	}

}
